/**
 * Provides the FirstLight LoadingState example in the GQE library.
 *
 * @file examples/demo/LoadingState.cpp
 * @author jacob Dix
 * @date 20110704 - Initial Release
 * @date 20110721 - Remove * from GetAsset() calls since it now returns TYPE&
 * @date 20110831 - Support new SFML2 snapshot changes
 */
#include "States/LoadingState.hpp"
#include "States/GameState.hpp"
#include <GQE/Core/assets/ImageAsset.hpp>
#include <GQE/Core/classes/App.hpp>
#include "Objects/ButtonObject.hpp"
#include "classes/ContentManager.hpp"

#include "classes/Command.hpp"
#include "Game_types.hpp"
LoadingState::LoadingState(GQE::App* theApp) :
    GQE::IState("Game",theApp),
    mMainConsole(32, 52),
    mDone(false)
{
}

LoadingState::~LoadingState(void)
{
}

void LoadingState::DoInit(void)
{
    // First call our base class implementation
    IState::DoInit();

    // Check our App pointer
    assert(NULL != mApp && "LoadingState::DoInit() bad app pointer");

    gConfig=new GQE::ConfigReader();
    gConfig->Read("resources/game.cfg");

    gButtonWidth=gConfig->GetUint32("ButtonInfo","Width",0);
    gButtonHeight=gConfig->GetUint32("ButtonInfo","Height",0);
    sf::String anNewString;
    anNewString.SetFont(mApp->mAssetManager.GetFont("MainFont")->GetAsset());
    anNewString.SetText("");
    anNewString.SetColor(sf::Color::Black);
    anNewString.SetSize(16);
    unsigned int anLine;
    for(anLine=0; anLine<gConfig->GetUint32("Main","Lines",20); anLine++)
    {
        anNewString.SetPosition(2,320-16*anLine);
        mMainStrings.push_back(anNewString);
    }
    mMainConsole.NewLine("Loading...");
    //Set up controls
    ButtonObject* newButton;
    newButton=new ButtonObject(mApp,"Button",GQE::ObjectVisible);
    mObjectManager.AddObject(newButton);
    newButton->SetText("OK");
    newButton->SetPosition(128,320,0);
    gMainWindow=&mMainConsole;
    //Make map
    gPlayer=new PlayerClass();
    mMainConsole.NewLine("Player class loaded...");
    // Make sure our update loop is only called 30 times per second
    mApp->SetUpdateRate(30.0f);
    gContent=new ContentManager();
    gContent->LoadList("resources/"+gConfig->GetString("Files","Content",""));
    mMainConsole.NewLine("Loaded content...");
    mDone=true;
}

void LoadingState::ReInit(void)
{

}

void LoadingState::HandleEvents(sf::Event theEvent)
{
    if(theEvent.Type==sf::Event::KeyPressed)
    {
        if(theEvent.Key.Code==sf::Key::Return && mDone)
        {
            // Add Menu State as the next active state
            mApp->mStateManager.AddActiveState(new(std::nothrow) GameState(mApp));
        }
    }
}

void LoadingState::UpdateFixed(void)
{
    unsigned int anLine;
    unsigned int anLineCount=gConfig->GetUint32("Main","Lines",20);
    for(anLine=0; anLine<anLineCount; ++anLine)
    {
        mMainStrings[anLine].SetText(mMainConsole.GetLine(mMainConsole.mLineNum-anLine-1));
    }
    mObjectManager.UpdateFixed();
}

void LoadingState::UpdateVariable(float theElapsedTime)
{
    mObjectManager.UpdateVariable(theElapsedTime);
}

void LoadingState::Draw(void)
{
    mObjectManager.UpdateLists();
    mApp->mWindow.Clear(sf::Color(255,255,255));
    std::vector<sf::String>::iterator anIter;
    for(anIter=mMainStrings.begin(); anIter!=mMainStrings.end(); anIter++)
    {
        mApp->mWindow.Draw((*anIter));
    }
    mObjectManager.Draw();
}

void LoadingState::Cleanup(void)
{
    mObjectManager.ClearObjects();
    // Last of all, call our base class implementation
    IState::Cleanup();

}

/**
 * Copyright (c) 2010-2011 Ryan Lindeman
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

